Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
@svgr/plugin-jsx
Advanced tools
@svgr/plugin-jsx is a plugin for SVGR (SVG to React) that transforms SVG files into JSX code. This allows developers to use SVGs as React components, making it easier to integrate and manipulate SVG graphics within React applications.
Basic SVG to JSX Transformation
Transforms a basic SVG string into JSX code that can be used as a React component.
const svgCode = '<svg><circle cx="50" cy="50" r="40" /></svg>';
const jsxCode = transform(svgCode, { plugins: ['@svgr/plugin-jsx'] });
console.log(jsxCode);
Customizing JSX Output
Allows customization of the JSX output using Babel presets and other configurations.
const svgCode = '<svg><circle cx="50" cy="50" r="40" /></svg>';
const jsxCode = transform(svgCode, { plugins: ['@svgr/plugin-jsx'], jsx: { babelConfig: { presets: ['@babel/preset-react'] } } });
console.log(jsxCode);
Adding Props to SVG Components
Automatically adds props to the SVG components, making them more flexible and reusable.
const svgCode = '<svg><circle cx="50" cy="50" r="40" /></svg>';
const jsxCode = transform(svgCode, { plugins: ['@svgr/plugin-jsx'], jsx: { expandProps: 'start' } });
console.log(jsxCode);
react-svg is a library that allows you to import SVG files as React components. It provides similar functionality to @svgr/plugin-jsx but focuses more on runtime usage rather than build-time transformations.
svg-react-loader is a Webpack loader that transforms SVG files into React components. It offers similar capabilities to @svgr/plugin-jsx but is specifically designed to work within the Webpack ecosystem.
react-inlinesvg is a React component that loads and injects SVGs inline. Unlike @svgr/plugin-jsx, it focuses on runtime loading and rendering of SVGs rather than transforming them into JSX at build time.
Transforms SVG into JSX.
npm install --save-dev @svgr/plugin-jsx
.svgrrc
{
"plugins": ["@svgr/plugin-jsx"]
}
@svgr/plugin-jsx
consists in three phases:
@svgr/babel-preset
transformationsYou can extend the Babel config applied in this plugin using jsx.babelConfig
config path:
// .svgrrc.js
module.exports = {
jsx: {
babelConfig: {
plugins: [
// For an example, this plugin will remove "id" attribute from "svg" tag
[
'@svgr/babel-plugin-remove-jsx-attribute',
{
elements: ['svg'],
attributes: ['id'],
},
],
],
},
},
}
Several Babel plugins are available:
@svgr/babel-plugin-add-jsx-attribute
@svgr/babel-plugin-remove-jsx-attribute
@svgr/babel-plugin-remove-jsx-empty-expression
@svgr/babel-plugin-replace-jsx-attribute-value
@svgr/babel-plugin-svg-dynamic-title
@svgr/babel-plugin-svg-em-dimensions
@svgr/babel-plugin-transform-react-native-svg
@svgr/babel-plugin-transform-svg-component
If you want to create your own, reading Babel Handbook is a good start!
MIT
FAQs
Transform SVG into JSX
The npm package @svgr/plugin-jsx receives a total of 6,718,168 weekly downloads. As such, @svgr/plugin-jsx popularity was classified as popular.
We found that @svgr/plugin-jsx demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.